import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBWorkTaiModel/YBWorkTaiReMenModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBWorkTaiNetWork/YBBaseWorkTaiNetWorkManager.dart';

class YBBaseProvider with ChangeNotifier{

  List<YBWorkTaiRemenModel> _reMenList = List<YBWorkTaiRemenModel>();
 
  List<YBWorkTaiRemenModel> get reMenModelList => _reMenList;

  RefreshController _easyRefreshController = RefreshController();

  RefreshController get easyRefreshController => _easyRefreshController;
  
  int _currentPage = 1;
  
  Future refreshData(String tagCode) async {
    _currentPage = 1;
    getReMenDataList(_currentPage, tagCode);
  }
  
  Future loadData(String tagCode) async {
    _currentPage++;
    getReMenDataList(_currentPage, tagCode);
  }
  
  Future getReMenDataList(int page,String tagCode) async {
    YBBaseWorkTaiNetWork.workTaiGongGaoArticleList({
      "sysCode":"xg_youbang",
      "tagCode":tagCode,
      "pageSize":20,
      "pageNo":page,
    },success:(data){
      _easyRefreshController.loadComplete();
      _easyRefreshController.refreshCompleted();
      int totalPage = data['totalPage'];

      if (page == 1){
        _reMenList.clear();
      }

      if (totalPage == page) {
         _easyRefreshController.loadNoData();
      }
      List resultList = data['result'];
      if (!ObjectUtil.isEmptyList(resultList)) {
          _reMenList.addAll(resultList.map((e){
            return YBWorkTaiRemenModel.fromJson(e);
          }).toList());
          notifyListeners();
      }
    },fail: (error){
      _easyRefreshController.refreshCompleted();
    });
  }
}


class YBZuzhiWenZhangWenZhangProvider with ChangeNotifier {

  List<YBWorkTaiRemenModel> _zuZhiList = List<YBWorkTaiRemenModel>();

  List<YBWorkTaiRemenModel> get zuZhiModelList => _zuZhiList;

  RefreshController _easyRefreshController = RefreshController();

  RefreshController get easyRefreshController => _easyRefreshController;

  int _currentPage = 1;

  Future refreshData() async {
    _currentPage = 1;
    getzuZhiDataList(_currentPage);
  }

  Future loadData() async {
    _currentPage++;
    getzuZhiDataList(_currentPage);
  }

  Future getzuZhiDataList(int page) async {
    YBBaseWorkTaiNetWork.workTaiGongGaoZuzhiWenZhang({
      "sysCode":"xg_youbang",
      "pageSize":20,
      "pageNo":page,
    },success:(data){
      print("----$data----");
      _easyRefreshController.loadComplete();
      _easyRefreshController.refreshCompleted();
      int totalPage = data['totalPage'];
      if (totalPage == page) {
        _easyRefreshController.loadNoData();
      }
      List resultList = data['result'];
      if (!ObjectUtil.isEmptyList(resultList)) {
        _zuZhiList.addAll(resultList.map((e){
          return YBWorkTaiRemenModel.fromJson(e);
        }).toList());
        print('--组织文章-$_zuZhiList--');
        notifyListeners();
      }
    },fail: (error){
      _easyRefreshController.refreshCompleted();
      _easyRefreshController.loadComplete();
    });
  }

}


class YBMyDeWenZhangProvider with ChangeNotifier {
  List<YBWorkTaiRemenModel> _woDeList = List<YBWorkTaiRemenModel>();

  List<YBWorkTaiRemenModel> get woDeModelList => _woDeList;

  RefreshController _easyRefreshController = RefreshController();

  RefreshController get easyRefreshController => _easyRefreshController;

  int _currentPage = 1;

  Future refreshData() async {
    _currentPage = 1;
    getWodeDataList(_currentPage);
  }

  Future loadData() async {
    _currentPage++;
    getWodeDataList(_currentPage);
  }

  Future getWodeDataList(int page) async {
    YBBaseWorkTaiNetWork.workTaiGongGaoWoDeWenZhang({
      "sysCode":"xg_youbang",
      "pageSize":20,
      "pageNo":page,
    },success:(data){
      print("----$data----");
      _easyRefreshController.loadComplete();
      _easyRefreshController.refreshCompleted();
      int totalPage = data['totalPage'];
      if (totalPage == page) {
        _easyRefreshController.loadNoData();
      }
      List resultList = data['result'];
      if (!ObjectUtil.isEmptyList(resultList)) {
        _woDeList.addAll(resultList.map((e){
          return YBWorkTaiRemenModel.fromJson(e);
        }).toList());
        print('--我的文章-$_woDeList--');
        notifyListeners();
      }
    },fail: (error){
      _easyRefreshController.refreshCompleted();
      _easyRefreshController.loadComplete();
    });
  }
}